﻿using System.ServiceProcess;
using System.Workflow.Runtime;
using System.ServiceModel;
using BneyBaruch.Ingest.MorningLesson.WorkflowProcessorServices;
using BneyBaruch.Ingest.Core.WorkflowCommunication;
using System.Diagnostics;

namespace BneyBaruch.Ingest.WorkflowManagementService
{
    public partial class IngestWorkflowManagementService : DebuggableService
    {
        #region Services Fields

        private MorningLessonWorkflowProcessor morningLessonWorkflowProcessor = null;
        private WorkflowServiceHostBase morningLessonWorkflowProcessorServiceHost = null;

        #endregion

        #region Constructors

        public IngestWorkflowManagementService()
        {
            InitializeComponent();
        }

        #endregion

        #region OnStart & OnStop Overrides

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            StartServicesHosts();
        }

        protected override void OnStop()
        {
            base.OnStop();
            StopServicesHosts();
        }

        #endregion

        #region Services Hosts Manipulation Methods

        private void StopServicesHosts()
        {
            if (morningLessonWorkflowProcessorServiceHost.State == CommunicationState.Opened)
                morningLessonWorkflowProcessorServiceHost.Close();
        }

        private void StartServicesHosts()
        {
            morningLessonWorkflowProcessor = new MorningLessonWorkflowProcessor();
            morningLessonWorkflowProcessorServiceHost = new WorkflowServiceHostBase(morningLessonWorkflowProcessor);
            morningLessonWorkflowProcessorServiceHost.Open();
        }

        #endregion
    }
}
